<?php

namespace addons\oil\controller;


use addons\sum\model\User;
use app\common\library\Auth;
use think\Db;

class Order extends Base
{

    protected $noNeedLogin = ['newlist','confirmByHexiao','detail_zz'];
    protected $noNeedRight = ['*'];

    /**
     * 最新优惠记录
     */
    public function newlist()
    {
        //用虚拟的数据
        $per_page=$this->request->request('per_page',10);
        $configModel = new \addons\sum\model\Config;
        $config = $configModel->where('name', 'share')->column('value', 'name');
        $shareConfig = json_decode($config['share'], true);
        $fake_count=$shareConfig['fake_count'];//虚拟数量
        $order_count=Db::name('oil_order')->where('status',2)->count();//实际数量
        $show_count=$fake_count+$order_count;//显示数量
        $fake_images=$shareConfig['fake_images'];//虚拟头像
        $fake_images_arr=explode(',',$fake_images);
        $bgcbor=[
            0=>[
                'bgc'=>"#6A95EB",
                'bor'=>"#C7DAFF"
            ],
            1=>[
                'bgc'=>"#6A95EB",
                'bor'=>"#C7DAFF"
            ],
            2=>[
                'bgc'=>"#6A95EB",
                'bor'=>"#C7DAFF"
            ]
        ];
        $list=[];
        for($i=0;$i<$per_page;$i++){
            $randomNumber = rand(0, 2);
            $item['num']=-rand(1, 10);
            $item['bgc']=$bgcbor[$randomNumber]['bgc'];
            $item['bor']=$bgcbor[$randomNumber]['bor'];
            if(!empty($fake_images_arr)){
                $fake_images_key = array_rand($fake_images_arr); // 随机获取一个键
                $item['src']=cdnurl($fake_images_arr[$fake_images_key],true);
                unset($fake_images_arr[$fake_images_key]);
            }else{
                $item['src']='';
            }
            $list[]=$item;
        }

        $return['show_count']=$show_count;
        $return['list']=$list;
        $this->success('获取成功', $return);
    }

    /**
     * 订单列表
     */
    public function index()
    {
        $params = $this->request->get();

        $this->success('订单列表', \addons\oil\model\Order::getList($params));
    }
    /**
     * 订单详情
     */
    public function detail()
    {
        $params = $this->request->get();
        $this->success('订单详情', \addons\oil\model\Order::detail($params));
    }

    /**
     * 订单详情-转赠的
     */
    public function detail_zz()
    {
        $params = $this->request->get();
        $this->success('订单详情', \addons\oil\model\Order::detail_zz($params));
    }

    /**
     * 预算订单
     */
    public function pre()
    {
        $params = $this->request->post();

        // 表单验证
        $this->oilValidate($params, get_class(), 'pre');

        $result = \addons\oil\model\Order::pre($params);

        if (isset($result['msg']) && $result['msg']) {
            $this->error($result['msg'], $result);
        } else {
            $this->success('计算成功', $result);
        }
    }
    /**
     * 创建订单
     */
    public function createOrder()
    {
//        $this->error('测试');
        $params = $this->request->post();
        // 表单验证
        $this->oilValidate($params, get_class(), 'createOrder');

        $order = \addons\oil\model\Order::createOrder($params);

        $this->success('下单成功', $order);
    }

    // 确认加油
    public function confirm()
    {
        $params = $this->request->post();
        // 表单验证
        $this->oilValidate($params, get_class(), 'confirm');

        $this->success('确认加油成功', \addons\oil\model\Order::operConfirm($params));
    }

    // 取消订单
    public function cancel()
    {
        $params = $this->request->post();

        // 表单验证
        $this->oilValidate($params, get_class(), 'cancel');

        $this->success('取消成功', \addons\oil\model\Order::operCancel($params));
    }

    // 分享转赠订单
    public function share()
    {
        $params = $this->request->post();
        // 表单验证
        $this->oilValidate($params, get_class(), 'share');
        $this->success('分享成功', \addons\oil\model\Order::operShare($params));
    }

    // 领取转赠订单
    public function receive()
    {
        $params = $this->request->post();
        // 表单验证
        $this->oilValidate($params, get_class(), 'receive');
        $this->success('领取成功', \addons\oil\model\Order::operReceive($params));
    }

    // 确认加油-by核销端
    public function confirmByHexiao()
    {
        $params = $this->request->post();
        // 表单验证
        $this->oilValidate($params, get_class(), 'confirm');

        $this->success('确认加油成功', \addons\oil\model\Order::operConfirmByHexiao($params));
    }

}
